package com.loveprogrammer.eternity.model.product.equipment;

import com.loveprogrammer.eternity.enums.EquipmentEnum;
import com.loveprogrammer.eternity.factory.EquipmentFactory;
import com.loveprogrammer.eternity.factory.suit.SuitEffect;
import com.loveprogrammer.eternity.factory.template.SuitTemplate;
import com.loveprogrammer.eternity.model.Character;
import com.loveprogrammer.eternity.model.effect.AbstractEffect;
import com.loveprogrammer.eternity.model.effect.Effect;
import com.loveprogrammer.eternity.model.product.Equipment;
import com.loveprogrammer.eternity.model.product.Goods;
import com.loveprogrammer.eternity.model.product.WearSuitEffect;

import java.io.Serializable;
import java.util.*;

/**
 * @version 1.0.0
 * @description: 装备的装备
 * @author: eric
 * @date: 2022-08-11 16:59
 **/
public class Equips implements Serializable {

    public  String string ="\t\t\t\t\t\t\t\t\t";

    public  String tt ="\t\t";

    private List<Equipment> all;

    // 套装效果
    private List<WearSuitEffect> suitEffects;

    public Equips(){
        all = new ArrayList<>();
    }

    public Equips(String string, Equipment weapon, Equipment leftRing, Equipment rightRing, Equipment breastplate, Equipment shoes,
                  Equipment helmet, Equipment glove, Equipment necklace, Equipment legArmor, Equipment belt, Equipment barcer,
                  Equipment shoulder, Equipment shield) {
        all = new ArrayList<>();
        this.string = string;
        this.weapon = weapon;
        this.leftRing = leftRing;
        this.rightRing = rightRing;
        this.breastplate = breastplate;
        this.shoes = shoes;
        this.helmet = helmet;
        this.glove = glove;
        this.necklace = necklace;
        this.legArmor = legArmor;
        this.belt = belt;
        this.barcer = barcer;
        this.shoulder = shoulder;
        this.shield = shield;
        initAll();
    }

    public void initAll() {
        all = new ArrayList<>();
        if(weapon != null){
            all.add(weapon);
        }
        if(leftRing != null){
            all.add(leftRing);
        }
        if(leftRing != null){
            all.add(leftRing);
        }
        if(breastplate != null)
            all.add(breastplate);
        if(shoes != null)
            all.add(shoes);
        if(helmet != null)
            all.add(helmet);
        if(glove != null)
            all.add(glove);

        if(necklace != null)
            all.add(necklace);
        if(legArmor != null)
            all.add(legArmor);

        if(belt != null)
            all.add(belt);
        if(barcer != null)
            all.add(barcer);

        if(shoulder != null)
            all.add(shoulder);
        if(shield != null)
            all.add(shield);
    }

    // 装备的武器
    private Equipment weapon;

    // 装备的戒指 左戒
    private Equipment leftRing;

    // 装备的戒指 右戒
    private Equipment rightRing;

    // 装备的护甲
    private Equipment breastplate;

    // 装备的鞋
    private Equipment shoes;

    // 装备的头盔
    private Equipment helmet;

    // 装备的手套
    private Equipment glove;

    // 装备的项链
    private Equipment necklace;

    // 装备的腿甲
    private Equipment legArmor;

    // 装备的腰带
    private Equipment belt;

    // 装备的护壁
    private Equipment barcer;

    // 装备的肩甲
    private Equipment shoulder;

    // 装备的盾牌
    private Equipment shield;

    public Equipment getWeapon() {
        return weapon;
    }

    public void setWeapon(Equipment weapon) {
        this.weapon = weapon;
    }

    public Equipment getBreastplate() {
        return breastplate;
    }

    public void setBreastplate(Equipment breastplate) {
        this.breastplate = breastplate;
    }

    public Equipment getLeftRing() {
        return leftRing;
    }

    public void setLeftRing(Equipment leftRing) {
        this.leftRing = leftRing;
    }

    public Equipment getRightRing() {
        return rightRing;
    }

    public void setRightRing(Equipment rightRing) {
        this.rightRing = rightRing;
    }

    public Equipment getShoes() {
        return shoes;
    }

    public void setShoes(Equipment shoes) {
        this.shoes = shoes;
    }

    public Equipment getHelmet() {
        return helmet;
    }

    public void setHelmet(Equipment helmet) {
        this.helmet = helmet;
    }

    public Equipment getGlove() {
        return glove;
    }

    public void setGlove(Equipment glove) {
        this.glove = glove;
    }

    public Equipment getNecklace() {
        return necklace;
    }

    public void setNecklace(Equipment necklace) {
        this.necklace = necklace;
    }

    public Equipment getLegArmor() {
        return legArmor;
    }

    public void setLegArmor(Equipment legArmor) {
        this.legArmor = legArmor;
    }

    public Equipment getBelt() {
        return belt;
    }

    public void setBelt(Equipment belt) {
        this.belt = belt;
    }

    public Equipment getBarcer() {
        return barcer;
    }

    public void setBarcer(Equipment barcer) {
        this.barcer = barcer;
    }

    public Equipment getShoulder() {
        return shoulder;
    }

    public void setShoulder(Equipment shoulder) {
        this.shoulder = shoulder;
    }

    public Equipment getShield() {
        return shield;
    }

    public void setShield(Equipment shield) {
        this.shield = shield;
    }

    public String prettyPrint() {
        return  string+"****装备信息**********************************************************\n"+
                tt + "武器：" + (getWeapon() == null ? "无" : getWeapon().toString()) +"\n"+
                tt + "盾牌：" + (getShield() == null ? "无" : getShield().toString()) +"\n"+
                tt + "胸甲：" + (getBreastplate() == null ? "无" : getBreastplate().toString()) +"\n"+
                tt + "头盔：" + (getHelmet() == null ? "无" : getHelmet().toString()) +"\n"+
                tt + "肩甲：" + (getShoulder() == null ? "无" : getShoulder().toString()) +"\n"+
                tt + "护壁：" + (getBarcer() == null ? "无" : getBarcer().toString()) +"\n"+
                tt + "手套：" + (getGlove() == null ? "无" : getGlove().toString()) +"\n"+
                tt + "腰带：" + (getBelt() == null ? "无" : getBelt().toString()) +"\n"+
                tt + "腿甲：" + (getLegArmor() == null ? "无" : getLegArmor().toString()) +"\n"+
                tt + "鞋子：" + (getShoes() == null ? "无" : getShoes().toString()) +"\n"+
                tt + "左戒：" + (getLeftRing() == null ? "无" : getLeftRing().toString()) +"\n"+
                tt + "右戒：" + (getRightRing() == null ? "无" : getRightRing().toString()) +"\n"+
                tt + "项链：" + (getNecklace() == null ? "无" : getNecklace().toString()) +"\n"+
                string+""+"************************************************************";
    }

    public Equipment findByPosition(EquipmentEnum equipmentEnum){
        if(equipmentEnum.equals(EquipmentEnum.WEAPON))
            return this.getWeapon();
        if(equipmentEnum.equals(EquipmentEnum.BARCER))
            return this.getBarcer();
        if(equipmentEnum.equals(EquipmentEnum.BELT))
            return this.getBelt();
        if(equipmentEnum.equals(EquipmentEnum.BREASTPLATE))
            return this.getBreastplate();
        if(equipmentEnum.equals(EquipmentEnum.GLOVES))
            return this.getGlove();
        if(equipmentEnum.equals(EquipmentEnum.HELMET))
            return this.getHelmet();
        if(equipmentEnum.equals(EquipmentEnum.LEFTRING))
            return this.getLeftRing();
        if(equipmentEnum.equals(EquipmentEnum.LEGARMOR))
            return this.getLegArmor();
        if(equipmentEnum.equals(EquipmentEnum.NECKLACE))
            return this.getNecklace();
        if(equipmentEnum.equals(EquipmentEnum.RIGHTRING))
            return this.getRightRing();
        if(equipmentEnum.equals(EquipmentEnum.SHIELD))
            return this.getShield();
        if(equipmentEnum.equals(EquipmentEnum.SHOES))
            return this.getShoes();
        if(equipmentEnum.equals(EquipmentEnum.SHOULDER))
            return this.getShoulder();
        return null;
    }

    /***
     * @Param [character,
     * equipment
     * ]
     * @return void
     **/
    public void checkSuitEffect(Character character, Equipment equipment) {
        int suitId = equipment.getSuitId();
        if(suitEffects == null){
            suitEffects = new ArrayList<>();
        }
        Optional<WearSuitEffect> suit = suitEffects.stream().filter(e -> e.getSuitId() == suitId).findFirst();
        WearSuitEffect suitEffect;
        if (suit.isPresent()) {
            suitEffect = suit.get();
            // 如果存在，只是替换
            if(suitEffect.getCurrentTypes().contains(equipment.getType())){
                return;
            }
            suitEffect.getCurrentTypes().add(equipment.getType());
            SuitEffect effectOld = suitEffect.getEffect();
            suitEffect.getSuitTemplate().getSuitEffects().stream()
                    .filter(e -> e.getCount() == suitEffect.getCurrentTypes().size())
                    .findFirst()
                    .ifPresent(effect -> {
                        character.resetSuitEffect(effectOld,effect);
                        suitEffect.setEffect(effect);
                    });
        }else{
            suitEffect = new WearSuitEffect();
            suitEffect.setSuitId(suitId);
            // 位置不能重复
            suitEffect.setCurrentTypes(new ArrayList<>(Collections.singletonList(equipment.getType())));
            SuitTemplate suitTemplate = EquipmentFactory.getSuitById(suitId);
            suitEffect.setSuitTemplate(suitTemplate);

            suitTemplate.getSuitEffects().stream()
                    .filter(e -> e.getCount() == 1)
                    .findFirst()
                    .ifPresent(effect -> {
                        character.resetSuitEffect(null,effect);
                        suitEffect.setEffect(effect);
                    });
            suitEffects.add(suitEffect);
        }
        this.setSuitEffects(suitEffects);
    }

    public List<WearSuitEffect> getSuitEffects() {
        return suitEffects;
    }

    public void setSuitEffects(List<WearSuitEffect> suitEffects) {
        this.suitEffects = suitEffects;
    }

    public void checkSuitEffectTakeOff(Character character, Equipment equipment) {
        int suitId = equipment.getSuitId();
        if(suitEffects == null){
            return;
        }
        Optional<WearSuitEffect> suit = suitEffects.stream().filter(e -> e.getSuitId() == suitId).findFirst();
        WearSuitEffect suitEffect;
        // 存在套装
        if (suit.isPresent()) {
            suitEffect = suit.get();
            // 如果存在对应位置的装备
            if(suitEffect.getCurrentTypes().contains(equipment.getType())){
                // 剔除掉
                suitEffect.getCurrentTypes().remove(equipment.getType());
                SuitEffect effectOld = suitEffect.getEffect();
                suitEffect.getSuitTemplate().getSuitEffects().stream()
                        .filter(e -> e.getCount() == suitEffect.getCurrentTypes().size())
                        .findFirst()
                        .ifPresent(effect -> {
                            character.resetSuitEffect(effectOld,effect);
                            suitEffect.setEffect(effect);
                        });
            }

        }
        this.setSuitEffects(suitEffects);
    }

    public List<Goods.GoodsEffectTemplate> checkEffects(){
        List<Goods.GoodsEffectTemplate> effects = new ArrayList<>();
        if(all.size() == 0){
            initAll();
        }
        for (Equipment equipment : all) {
            if(equipment.getEffects().size() > 0){
                effects.addAll(equipment.getEffects());
            }
        }
        return effects;
    }

    public List<Equipment> getAll() {
        return all;
    }
}
